<template>
  <div class="address">
    <van-nav-bar
      title="新增地址"
      left-text="返回"
      right-text="Mine"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
      @change-detail="onChangeDetail"
    />
  </div>
</template>

<script>
import { Toast } from "vant";
import { areaList } from "@vant/area-data"; // 是级联数据
import { addaddress } from "@/api/address";
export default {
  data() {
    return {
      areaList,
      searchResult: [],
    };
  },
  computed: {},
  watch: {},

  methods: {
    async onSave(e) {
      console.log(e);
      let addre = e.province + "-" + e.city + "-" + e.county + e.addressDetail;
      console.log(addre);
      this.$store.commit("address/savereceiver", e.name);
      this.$store.commit("address/savemobile", e.tel);
      this.$store.commit("address/saveregions", addre);

      const result = await addaddress({
        receiver: this.$store.state.address.receiver,
        mobile: this.$store.state.address.mobile,
        regions: this.$store.state.address.regions,
      });
      if (result.code == "success") {
        Toast(result.message);
        this.$router.push("addresslist");
      } else {
        Toast.fail(result.message);
      }
    },
    onDelete() {
      Toast("delete");
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: "黄龙万科中心",
            address: "杭州市西湖区",
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
    onClickLeft() {
      this.$router.go(-1);
      Toast("返回");
    },
    onClickRight() {
      this.$router.push("Mine");
      Toast("Mine");
    },
  },
  created() {},
  mounted() {},
  components: {},
};
</script>
<style scoped></style>
